package com.scau.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.scau.dao.BaseDao;
import com.scau.dao.ISerDao;
import com.scau.util.DateTransform;
import com.scau.vo.Service;
import com.scau.vo.ServiceCategory;

/**
 * @author Jinglun
 * @create 2017年3月16日
 */
public class SerDaoImpl extends BaseDao implements ISerDao {

	private Session session = null;
	private Transaction tx = null;
	private String hql = "";
	
	// 增加具体服务
	@Override
	public boolean saveService(Service service) {
		session = getSession();
		tx = session.beginTransaction();
		hql = "from Service service where service.name = '"
				+ service.getName() + "'";
		Service exist = (Service) session.createQuery(hql).uniqueResult();
		if (exist != null) { // 已存在
			tx.commit();
			session.close();
			return false;
		} else {
			// request.getParameter(select的name属性名)用来获取前端select数据
			// HttpServletRequest request = ServletActionContext.getRequest();
			// String id = request.getParameter("serCateId");
			String id = service.getSerCateName();
			// 转换成int型
			Integer scId = Integer.parseInt(id);
			// 从serCateId获取名称
			String hql2 = "from ServiceCategory sc where sc.id = '" + id + "'";
			ServiceCategory serC = (ServiceCategory) session.createQuery(hql2)
					.uniqueResult();
			service.setServiceCategory(serC);
			service.setSerCateName(serC.getName());
			session.save(service);
			tx.commit();
			session.close();
			return true;
		}
	}

	// 查看所有具体服务
	@Override
	public List<Service> listService() {
		session = getSession();
		tx = session.beginTransaction();
		hql = "from Service service ";
		Query query = session.createQuery(hql);
		List<Service> serviceList = query.list();
		tx.commit();
		session.close();
		return serviceList;
	}

	// 模糊查询服务
	@Override
	public List<Service> findService(String message) {
		session = getSession();
		tx = session.beginTransaction();
		hql = "from Service ser where ser.serviceName like '%" + message + "%'";
		Query query = session.createQuery(hql);
		List<Service> list = query.list();
		System.out.println("查询出的list为：" + list);
		tx.commit();
		session.close();
		return list;
	}

	// 更新具体服务
	@Override
	public boolean updateService(Service service) {
		session = getSession();
		tx = session.beginTransaction();
		
		Service ser = (Service) session.get(Service.class, service.getId());
		
		if (ser != null) {
			// 更新，名称，注释，价格，修改时间
			ser.setName(service.getName());
			ser.setPrice(service.getPrice());
			ser.setRemark(service.getRemark());
			ser.setGmtModified(service.getGmtModified());

			/*
			 * 注意！！
			 * 我前端的select框里，service.serCateName存的是服务类型的id
			 * 因此这里的service.getSerCateName()返回一个服务类型id
			 */
			String hql2 = "from ServiceCategory sc where sc.id = '" + service.getSerCateName() + "'";
			ServiceCategory serC = (ServiceCategory) session.createQuery(hql2)
					.uniqueResult();
			System.out.println("serC:"+serC);
			ser.setServiceCategory(serC);
			ser.setSerCateName(serC.getName());
			
			tx.commit(); 
			session.close();
			System.out.println("服务类型：" + service.getName()
					+ "更新成功！");
			return true;
		} else {
			tx.commit();
			session.close();
			System.out.println("服务类型：" + service.getName()
					+ "更新失败！");
			return false;
		}
	}

	// 删除服务
	@Override
	public boolean deleteService(int id) {
		try {
			session = getSession();
			tx = session.beginTransaction();
			// 加载要删除的实体
			Service ser = (Service) session.get(Service.class, id);
			session.delete(ser);
			tx.commit();
			session.close();
			System.out.println("服务id：" + id + "删除成功！");
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.commit();
			session.close();
			System.out.println("服务id：" + id + "删除失败！");
			return false;
		}
	}
	
	public Service getServiceById(int id){
		session = getSession();
		
		Service s = (Service) session.get(Service.class, id);
		return s;
	}

}
